//
//  HMStatusOriginalView.swift
//  MicroBlog
//
//  Created by Apple on 16/9/23.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SDWebImage
import YYText
//1.原创微博视图
class HMStatusOriginalView: UIView {
    //记录原创微博底部约束
    var originalBottomConstraint: Constraint?
    //子控件赋值
    var statusViewModel: HMStatusViewModel? {
        didSet{
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            if let userImaeUrl =  statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImage(with: URL(string: userImaeUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
//            contentLabel.text = statusViewModel?.status?.text
            //原创微博富文本
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            mbrankImageView.image = statusViewModel?.mbrankImage
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            sourceLabel.text = statusViewModel?.sourceContent
            timeLabel.text = statusViewModel?.timeContent
            //卸载上次约束
            originalBottomConstraint?.uninstall()
            //判断原创微博视图是否存在
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //有图
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                   originalBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTableViewCellMargin).constraint
                })
                pictureView.picUrls = statusViewModel?.status?.pic_urls
            } else {
                //无图
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    originalBottomConstraint = make.bottom.equalTo(contentLabel).offset(HMHomeTableViewCellMargin).constraint
                })
            }
        }
    }
    //懒加载控件
    //用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView()
    //昵称
    private lazy var screenNameLabel: UILabel = {
       let label = UILabel(textColor: UIColor.darkGray, fontSIze: 14)
        return label
    }()
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSIze: 11)
        return label
    }()
    //来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSIze: 14)
        return label
    }()
    //微博内容
    private lazy var contentLabel: YYLabel = {
//        let label = UILabel(textColor: UIColor.darkGray, fontSIze: HMHomeTableViewCellFontSize)
        let label = YYLabel()
        //设置最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HMHomeTableViewCellMargin
        label.numberOfLines = 0
        return label
    }()
    //配图
    private lazy var pictureView: HMStatusPictureView = {
        let view = HMStatusPictureView()
        //配图和背景色相同
        view.backgroundColor = self.backgroundColor

        return view
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //添加控件,设置约束
    private func setupUI() {
        backgroundColor = UIColor.white
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置约束
        userImageView.snp_makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HMHomeTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(HMHomeTableViewCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HMHomeTableViewCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(HMHomeTableViewCellMargin)
            make.bottom.equalTo(userImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMHomeTableViewCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView.snp_bottom).offset(HMHomeTableViewCellMargin)
            make.leading.equalTo(userImageView)
            make.width.equalTo(ScreenWidth - 2 * HMHomeTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(HMHomeTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        //设置向上的约束
        self.snp_makeConstraints { (make) in
            originalBottomConstraint = make.bottom.equalTo(pictureView).constraint
        }
        //设置点击高亮效果执行的闭包
        contentLabel.highlightTapAction = { (containerView,attributedSting,range,rect) in
            //根据匹配的范围获取对应的字符串
            let text = (attributedSting.string as NSString).substring(with: range)
            if text.hasPrefix("http") {
                print("你点击的是链接")
            }
            print(text)
        }
    }
}
